package cn.itcast.bos.web.action.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.itcast.bos.crm.service.customer.CustomerService;
import cn.itcast.bos.service.auth.FunctionService;
import cn.itcast.bos.service.auth.RoleService;
import cn.itcast.bos.service.bc.DecidedZoneService;
import cn.itcast.bos.service.bc.RegionService;
import cn.itcast.bos.service.bc.StaffService;
import cn.itcast.bos.service.bc.SubAreaService;
import cn.itcast.bos.service.qp.NoticeBillService;
import cn.itcast.bos.service.qp.WorkOrderManageService;
import cn.itcast.bos.service.user.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

@SuppressWarnings("all")
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
	protected T model;

	// 各种业务层注入
	@Autowired
	protected UserService userService;

	@Autowired
	protected StaffService staffService;

	@Autowired
	protected RegionService regionService;

	@Autowired
	// 该注解 对于代理类无效
	protected SubAreaService subAreaService;

	@Autowired
	protected DecidedZoneService decidedZoneService;

	@Autowired
	protected CustomerService customerService;

	@Autowired
	protected NoticeBillService noticeBillService;

	@Autowired
	protected WorkOrderManageService workOrderManageService;

	@Autowired
	protected FunctionService functionService;

	@Autowired
	protected RoleService roleService;

	// @Autowired
	// protected UserService userService;

	public T getModel() {
		return model;
	}

	public BaseAction() {
		Class<T> modelClass = null;
		// 对model进行实例化， 通过子类 类声明的泛型
		Type superclass = this.getClass().getGenericSuperclass();
		// 转化为参数化类型
		if (superclass instanceof ParameterizedType) {
			// 没有走代理 本类
			ParameterizedType parameterizedType = (ParameterizedType) superclass;
			modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		} else {
			// 执行代理 获取父类 适合二级代理
			ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getSuperclass().getGenericSuperclass();
			modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		}
		// 获取一个泛型参数
		try {
			model = modelClass.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	// 值栈获取

	public ValueStack getValueStack() {
		return ActionContext.getContext().getValueStack();
	}

	public void push(Object obj) {
		getValueStack().push(obj);// 将对象压入到栈顶...
	}

	// servlet request response
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	// getParameter
	public Object getParameter(String key) {
		return getRequest().getParameter(key);
	}

	// session 获取session 指定key 对应数据
	public Object getSessionData(String key) {
		return getRequest().getSession().getAttribute(key);
	}

	// remove session数据
	public void removeSessionData(String key) {
		getRequest().getSession().removeAttribute(key);
	}

	// session session 存储数据
	public void setSessionData(String key, Object obj) {
		getRequest().getSession().setAttribute(key, obj);
	}

	// 分页参数封装 当前页面以及每页显示记录数 easyui 插件 datagrid 发送
	protected int page;// 父类自动封装当前页面
	protected int rows;// 分页查询每页记录数

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	// json 对象的封装
	//---mxf 增加start
	
	private Object jsonResult;
	
	//会在值栈中找到Action，从Action中获取key为jsonResult的对象数据
	public Object getJsonResult() {
		return jsonResult;
	}
	
	
	//end

}
